<?php

namespace app\admin\controller;

use app\admin\model\SystemAdmin;
use app\common\constants\AdminConstant;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use EasyAdmin\tool\CommonTool;
use jianyan\excel\Excel;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="QQ管理2")
 */
class Qq2 extends AdminController
{
    protected $sort = [
        'is_read'   =>  'asc',
        'id'        => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Qq2();
        
    }

    /**
     * @NodeAnotation(title="QQ列表2")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFieds')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();

            $user = SystemAdmin::find(session('admin.id'));
            //不受限制的qq账号
            $qqs = [18,19];
            if ($user->id == AdminConstant::SUPER_ADMIN_ID || in_array($user->id, $qqs)){
                $where[] = ['id', '>', 0];
            }else if(empty($user->zqq)){
                $where[] = ['id', '<', 0];
            }else{
                $zqq = explode(',', $user->zqq);
                $where[] = ['zqq', 'in', $zqq];
            }

            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="标记为已跟踪")
     */
    public function read()
    {
        $id = $this->request->param('id/d');
        $row = $this->model->where('is_read', 0)->find($id);
        if (empty($row)){
            return;
        }
        try {
            $row->is_read = 1;
            $row->save();
        }catch (\Exception $e){}
        return;
    }

    /**
     * @NodeAnotation(title="导出")
     */
    public function export()
    {
        list($page, $limit, $where) = $this->buildTableParames();
        $list = $this->model
            ->where($where)
            ->limit(100000)
            ->order($this->sort)
            ->column('qq','id');
        $data = '';
        foreach ($list as $k => $v){
            $data .= $v.'@qq.com'.PHP_EOL;
        }
        header("Content-Type: application/octet-stream");
        header('Content-Disposition: inline;filename="1.txt"');
        header('Cache-Control: max-age=0');
        echo $data;
        exit();
    }
}